package com.zhz.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhz.domain.SysPrivilege;
import com.zhz.model.R;
import com.zhz.service.SysPrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author ：zhz
 * @date ：Created in 2020/12/21
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description: 权限管理的控制器=》1、查询 2、修改 3、新增 4、删除
 * 权限管理的权限：
 * 查询：sys_privilege_query
 * 修改：sys_privilege_update
 * 新增：sys_privilege_create
 * 删除：sys_privilege_delete
 **/
@RestController
@RequestMapping("/privileges")
@Api(tags = "权限的管理")
public class SysPrivilegeController {

    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    @GetMapping
    @ApiOperation(value = "分页查询权限数据")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "current", value = "当前页"),
                    @ApiImplicitParam(name = "size", value = "每页显示的大小")
            }
    )
    @PreAuthorize("hasAuthority('sys_privilege_query')")
    public R<Page<SysPrivilege>> findByPage(@ApiIgnore Page<SysPrivilege> page) {//返回为什么忽略此参数/操作的说明
        // 查询时，我们将最近新增的、修改的数据优先展示-> 排序->lastUpdateTime
        page.addOrder(OrderItem.desc("last_update_time"));
        //分页
        Page<SysPrivilege> sysPrivilegePage = sysPrivilegeService.page(page);
        return R.ok(sysPrivilegePage);
    }

    @PostMapping
    @ApiOperation(value = "新增一个权限")
    @PreAuthorize("hasAuthority('sys_privilege_create')")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "sysPrivilege", value = "sysPrivilege 的json数据")
            }
    )
    public R add(@RequestBody @Validated SysPrivilege sysPrivilege) {
        return sysPrivilegeService.save(sysPrivilege)?R.ok("新增成功"):R.fail("新增失败");
    }

    @PostMapping
    @ApiOperation(value = "修改一个权限")
    @PreAuthorize("hasAuthority('sys_privilege_update')")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "sysPrivilege", value = "sysPrivilege 的json数据")
            }
    )
    public R updateSysPrivilege(@RequestBody @Validated SysPrivilege sysPrivilege) {
        return sysPrivilegeService.updateById(sysPrivilege) ? R.ok("修改成功") : R.fail("新增失败");
    }
}
